// pages/userbbs/userbbs.js\
const db = wx.cloud.database()
const app = getApp()
Page({

  data: {
    isAdmin: false,
    message: [],
    myMessage:'',
    value:''
  },
  onChangeMessage(e){
   
    this.setData({
      myMessage: e.detail
    })
  },
  deleMessage(e){
  
    db.collection("notifyMessage").where({
      _id: e.target.id
    }).remove()
  },
  // sendMessage(){
  //   db.collection("bbsmessage").add({
  //     data:{
  //       message: this.data.myMessage,
  //       name: app.globalData.name,
  //       avatarUrl: app.globalData.avatarUrl,
  //       time: new Date(),
  //       identity: app.globalData.identity
  //     }
  //   }).then(res=>{
  //     this.setData({
  //       value:''
  //     })
  //   })

  // },
  async getMessage(){
    let message = await db.collection("notifyMessage").count()
    message = message.total
    let all_message = []
    for (let i = 0; i < message; i+= 20){
      let  list_message = await db.collection("notifyMessage").skip(i).get()
      all_message = all_message.concat(list_message.data)
    }
    this.setData({
      message: all_message
    })
  },

  onLoad:  function (options) {
    
    this.getMessage()
    db.collection("notifyMessage").watch({
      onChange: res=>{
        this.setData({
          message: res.docs
        })
      },
      onError:res => {

      }
    })
    if(app.globalData.identity=='管理员'){
      this.setData({
        isAdmin: true
      })
    }

  },
  
 


  onShow: function () {
    
  },
  onShareAppMessage: function () {

  }
})